package com.jojo.android.zxlib.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.jojo.android.zxlib.R;

/**
 * 创建者 zx
 * 创建时间 2020/12/4 12:06
 */
public class LoadMoreListView extends ListView implements AbsListView.OnScrollListener {
    private final String TAG = "LoadMoreListView";

    private ProgressBar loadingProgress;
    private TextView noMoreTips; //没有更多
    private boolean isHasMore = true;//是否还有更多
    private boolean isLoading;//是否加载中
    private OnLoadMoreListener onLoadMoreListener;
    private boolean enabledLoadMore = true;
    private View footerView;
    private int visibleItemPosition;

    public LoadMoreListView(Context context) {
        super(context);
        init();
    }

    public LoadMoreListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public LoadMoreListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        footerView = LayoutInflater.from(getContext()).inflate(R.layout.foot_layout, null, false);
        loadingProgress = footerView.findViewById(R.id.loading_progress);
        noMoreTips = footerView.findViewById(R.id.noMore_tips_tv);
        addFooterView(footerView);
        this.setOnScrollListener(this);
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (enabledLoadMore) {
            if (visibleItemPosition >= getAdapter().getCount() - 1
                    && scrollState == OnScrollListener.SCROLL_STATE_IDLE && isHasMore && !isLoading) {
                loadingProgress.setVisibility(View.VISIBLE);
                isLoading = true;
                if (onLoadMoreListener != null) {
                    onLoadMoreListener.onLoadMore(LoadMoreListView.this);
                }
            }
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        visibleItemPosition = firstVisibleItem + visibleItemCount;
    }


    public ProgressBar getLoadingProgress() {
        return loadingProgress;
    }


    public TextView getNoMoreTips() {
        return noMoreTips;
    }


    public boolean isHasMore() {
        return isHasMore;
    }

    public void setHasMore(boolean hasMore) {
        if (noMoreTips != null) {
            if (hasMore) {
                noMoreTips.setVisibility(GONE);
            } else {
                noMoreTips.setVisibility(VISIBLE);
            }
        }
        isHasMore = hasMore;
    }

    public boolean isLoading() {
        return isLoading;
    }

    public void setLoading(boolean loading) {
        if (loadingProgress != null) {
            if (loading) {
                loadingProgress.setVisibility(VISIBLE);
            } else {
                loadingProgress.setVisibility(GONE);
            }
        }
        isLoading = loading;
    }

    public boolean isEnabledLoadMore() {
        return enabledLoadMore;
    }

    public void setEnabledLoadMore(boolean enabledLoadMore) {
        this.enabledLoadMore = enabledLoadMore;
    }


    public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
        this.onLoadMoreListener = onLoadMoreListener;
    }


    public interface OnLoadMoreListener {
        void onLoadMore(LoadMoreListView recyclerView);
    }
}
